let { loginController, registerController, userFindByIdController, openController } = require('../controller/publicController');

let jwt = require('jsonwebtoken')


loginService = async (req, res) => {
    let { role, userNumber, passwd } = req.body;
    if (role && userNumber && passwd) {
        let idResults = await userFindByIdController(role, userNumber)
        if (idResults.results) { // 666abc888 dlrblovewhr
            switch (idResults.results.state) {
                case '通过':
                    let result = await loginController(role, userNumber, passwd)
                    if (result) {
                        //                        将用户的身份 和账号进行存储  干扰字符串  时间
                        const token = jwt.sign(
                            {
                                role,
                                userNumber
                            }, 'PRIMARYKEY',
                            { expiresIn: '1h' })
                        res.json({
                            code: 200,
                            message: "登录成功",
                            token: "Bearer " + token
                        })
                    } else {
                        res.json({
                            code: 201,
                            message: "登录失败，账号或密码不正确"
                        })
                    }
                    break;
                case '未通过':
                    res.json({
                        code: 201,
                        message: "登录失败，账号未通过，请联系管理员或者讲师",
                        unJoin: idResults.results.unJoin,
                    })
                    break;
                case '待审核':
                    res.json({
                        code: 201,
                        message: "登录失败，账号未被审核，请联系管理员或者讲师"
                    })
                    break;
            }
        } else {
            res.json({
                code: 201,
                message: "登录失败，该账号不存在或者角色选择不正确"
            })
        }

    } else {
        res.json({
            code: 201,
            message: "登录失败，参数不正确"
        })
    }
}

registerService = async (req, res, next) => {
    let { role, userNumber, passwd, name, gender, age } = req.body;
    if (role, userNumber && passwd && name && gender && age) {
        let idResults = await userFindByIdController(role, userNumber)
        if (idResults.results) {
            res.json({
                code: 201,
                message: "注册失败,该账号已经存在"
            })
        } else {
            let results = await registerController(role, userNumber, name, gender, age);
            if (results.results) {
                res.json({
                    code: 200,
                    message: "账号注册成功"
                })
            } else {
                res.json({
                    code: 201,
                    message: "账号注册失败,未知原因"
                })
            }
        }
    } else {
        res.json({
            code: 201,
            message: "账号注册失败,参数不正确"
        })
    }
}

openService = async (req, res, next) => {
    let { role, userNumber, state } = req.body;
    if (role && userNumber) {
        let userResult = await userFindByIdController(role, userNumber);
        if (userResult.results) {
            switch (state) {
                case '通过':
                    let result1 = await openController(role, userNumber, state)
                    if (result1.results?.matchedCount == 1) {
                        res.json({
                            code: 200,
                            message: "账号授权成功"
                        })
                    } else {
                        res.json({
                            code: 201,
                            message: "账号授权失败,未知原因，请联系管理员"
                        })
                    }
                    break;
                case '未通过':
                    let { unJoin } = req.body;
                    let result2 = await openController(role, userNumber, state, unJoin)
                    if (result2.results?.matchedCount == 1) {
                        res.json({
                            code: 200,
                            message: "账号拒绝成功"
                        })
                    } else {
                        res.json({
                            code: 201,
                            message: "账号拒绝失败,未知原因，请联系管理员"
                        })
                    }
                    break;
                default:
                    res.json({
                        code: 201,
                        message: "账号授权失败,授权参数不正确(可选通过,未通过)"
                    })
            }
        } else {
            res.json({
                code: 201,
                message: "账号授权失败,该账号不存在"
            })
        }
    } else {
        res.json({
            code: 201,
            message: "账号授权失败,参数不正确"
        })
    }
}

module.exports = {
    loginService,
    registerService,
    openService
}